package com.team6.cet.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team6.cet.entity.ExaminationInformation;
import com.team6.cet.entity.ExamineeInformation;
import com.team6.cet.entity.IStatusMessage;
import com.team6.cet.entity.OrdinaryUser;
import com.team6.cet.entity.Quasinfo;
import com.team6.cet.entity.ResponseResult;
import com.team6.cet.entity.Userenrollment;
import com.team6.cet.mapper.ExamineeInformationMapper;
import com.team6.cet.service.ExaminationInformationService;
import com.team6.cet.service.ExamineeInformationService;
import com.team6.cet.service.OrdinaryUserService;
import com.team6.cet.service.UserenrollmentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@CrossOrigin(origins = "*")
@RestController
@EnableAutoConfiguration
@Api(tags = "报考信息api")
@Validated
/**
 * 用户信息控制类*/
public class ExamineeInformationController {

	@Autowired
	private ExamineeInformationService eService;
	@Autowired
	private UserenrollmentService uService;
	@Autowired
	private OrdinaryUserService userService;
	@Autowired  
	private ExaminationInformationService examService;
	@Autowired  
	private ExamineeInformationMapper eMapper;
	
	@ApiOperation(value = "查看准考信息")
	@GetMapping("/examineeinfo")
	public Object getUserenrollment()
	{
		Map map=new HashMap();
		List<ExamineeInformation> eList=eService.findAll();
		map.put("data", eList);
		map.put("code", 0);
		map.put("count",eMapper.findNum());
		return  map;
	}
	
	@ApiOperation(value = "添加准考信息")
	@PostMapping("/addexamineeinfo")
	public ResponseResult addexamineeinfo(@ApiParam(required = true, value = "添加考试信息")@RequestBody @Valid ExamineeInformation eInfo)
	{
		 
		eService.insertExamineeinfo(eInfo);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
		responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
		responseResult.setMessage("添加成功");
		responseResult.setObj(eInfo);
		return responseResult;
	}
	
	@ApiOperation(value = "修改准考信息")
	@PostMapping("/updataexamineeinfo")
	public ResponseResult updataexamineeinfo(@ApiParam(required = true, value = "添加考试信息")@RequestBody @Valid ExamineeInformation eInfo)
	{
		Map map=new HashMap();
		eService.updateExamineeinfo(eInfo);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
		responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
		responseResult.setMessage("修改成功");
		return responseResult;
	}
	
	@ApiOperation(value = "删除准考信息")
	@GetMapping("/delexamineeinfo")
	public ResponseResult delexamineeinfo(@RequestParam("id")Integer id)
	{
		Map map=new HashMap();
		eService.delExamineeinfo(id);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
		responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
		responseResult.setMessage("成功");
		return responseResult;
	}
	
	@ApiOperation(value = "根据id查询准考信息")
	@GetMapping("/examineeinfobyid")
	public ResponseResult examineeinfobyid(@RequestParam("id")Integer id)
	{
		ExamineeInformation eInfo=eService.getExamineeinformationbyId(id);
		eInfo.setPaymentState("已支付");
		eService.updateExamineeinfo(eInfo);
		ExaminationInformation exam=examService.findExamById(eInfo.getExaminationInformationID());
		Userenrollment user=uService.findByidNum(eInfo.getIdNumber());
		Quasinfo quasinfo=new Quasinfo( exam, user, eInfo);
		ResponseResult responseResult = new ResponseResult();
		responseResult.setCode(IStatusMessage.SystemStatus.ERROR.getCode());
		responseResult.setCode(IStatusMessage.SystemStatus.SUCCESS.getCode());
		responseResult.setObj(quasinfo);
		return  responseResult;
	}
	
	@ApiOperation(value = "根据idnum查询准考信息")
	@GetMapping("/examineeinfobyidnum")
	public Object examineeinfobyidnum(@RequestParam("idnum")String idnum)
	{
		 
		Map map=new HashMap();
		List<ExamineeInformation> eList=eService.getExamineeinformationbyidnum(idnum);
		List<Quasinfo> quasinfos=new LinkedList<Quasinfo>();
		for(ExamineeInformation eInfo :eList)
		{
			ExaminationInformation exam=examService.findExamById(eInfo.getExaminationInformationID());
			Userenrollment user=uService.findByidNum(eInfo.getIdNumber());
			Quasinfo quasinfo=new Quasinfo( exam, user, eInfo);
			quasinfos.add(quasinfo);
		}
		map.put("data", quasinfos);
		map.put("code", 0);
		map.put("count", 1);
		return  map;
	}
	
	
 	@ApiOperation(value = "根据id查询准考信息")
	@GetMapping("/getbyidnum")
	public Object changeinfobyidnum(@RequestParam("idnum")String idnum)
	{
		Map map=new HashMap();
		List<ExamineeInformation> eList=eService.getExamineeinformationbyidnum(idnum);
		map.put("data", eList);
		map.put("code", 0);
		map.put("count", 1);
		return  map;
	} 
	
}
